<?php 
include ("../includes/config_conexao.php");

$idTecnico = $_GET['idTecnico'];

$sql = "SELECT s.id_servico, c.desc_cliente,
		      s.data_prev_servico, s.hr_prev_exec_servico
		FROM servico s, atendimento a, historico_atendimento ha, cliente c
		WHERE s.status_servico = 'A'
			AND s.id_servico = a.id_servico
			AND a.id_atendimento = ha.id_atendimento
			AND ha.id_historico = (select id_historico 
						    from historico_atendimento
						    where id_atendimento = a.id_Atendimento
						    order by data_inc_historico desc
						    limit 1)
			AND s.id_cliente = c.id_cliente";

$sql .= !empty($idTecnico) ? " AND s.id_usuario = ".$idTecnico : "";

$arr = array();
$result = mysql_query($sql);

while($linha = mysql_fetch_array($result)){
	$arr[] = array(
				'id' => $linha['id_servico'],
				'title' => $linha['desc_cliente'],
				'start' => $linha['data_prev_servico']." ".$linha['hr_prev_exec_servico'],
				'end' => $linha['data_prev_servico']." ".$linha['hr_prev_exec_servico'],
				'allDay' => false
				);
}

echo json_encode($arr);

?>
